package com.yami.shop.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.beust.jcommander.internal.Lists;
import com.yami.shop.bean.model.BlockchainShop;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.ShopChainApply;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.DataWebService;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.BlockchainShopService;
import com.yami.shop.service.ShopChainApplyService;
import com.yami.shop.service.ShopDetailService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @Author: LSR
 * @Date: 2022/12/28 10:00
 */

@RestController
@RequestMapping("/admin/shopChainApply")
public class ShopChainApplyController {
    @Autowired
    private ShopChainApplyService shopChainApplyService;
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private ShopConfig shopConfig;
    @Autowired
    private DataWebService dataWebService;
    @Autowired
    private BlockchainShopService blockchainShopService;

    @GetMapping("/list")
    @Operation(summary = "店铺链账号配置列表", description = "店铺链账号配置列表")
    public ServerResponseEntity<IPage<ShopChainApply>> list(PageParam<ShopChainApply> page, ShopChainApply shopChainApply) {
        LambdaQueryWrapper<ShopChainApply> tagQueryWrapper = new LambdaQueryWrapper<>();
        tagQueryWrapper
                .like(StrUtil.isNotEmpty(shopChainApply.getChainName()),ShopChainApply::getChainName,shopChainApply.getChainName())
                .like(StrUtil.isNotEmpty(shopChainApply.getShopName()),ShopChainApply::getShopName,shopChainApply.getShopName())
                .ne(ShopChainApply::getStatus,0).orderByAsc(ShopChainApply::getStatus).orderByDesc(ShopChainApply::getCreateTime);
        IPage<ShopChainApply> tagList = shopChainApplyService.page(page,tagQueryWrapper);
        return ServerResponseEntity.success(tagList);
    }



    @PutMapping
    @Operation(summary = "审核", description = "修改")
    public ServerResponseEntity<Void> update(@RequestBody ShopChainApply shopChainApply) {
        if(shopChainApply.getStatus()!=3 && shopChainApply.getStatus()!=2){
            return ServerResponseEntity.showFailMsg("参数异常");
        }
        ShopChainApply byId = shopChainApplyService.getById(shopChainApply.getId());
        if(byId.getStatus()!=1){
            return ServerResponseEntity.showFailMsg("状态数据异常");
        }
        if(shopChainApply.getStatus()==3 && StrUtil.isEmpty(shopChainApply.getRemark())){
            return ServerResponseEntity.showFailMsg("请填写驳回原因");
        }
        if(shopChainApply.getStatus()==2){

            BlockchainShop blockchainShop = blockchainShopService.getOne(Wrappers.lambdaQuery(BlockchainShop.class)
                    .eq(BlockchainShop::getShopId, byId.getShopId()).eq(BlockchainShop::getBlockchainCode, byId.getChainCode()));

            if (blockchainShop == null) {
                return ServerResponseEntity.showFailMsg("该链配置信息查询为空");
            }
            if (blockchainShop.getIsCoin() == 1) {
                return ServerResponseEntity.showFailMsg("该链配置已存在");
            }
            ShopDetail shopDetailByShopId = shopDetailService.getShopDetailByShopId(byId.getShopId());
            Map<String, String> stringObjectMap = new HashMap<>();
            stringObjectMap.put("platformCode","dtc");
            stringObjectMap.put("shopId",blockchainShop.getShopId().toString());
            if ("dt".equals(byId.getChainCode())) {
                stringObjectMap.put("roleCode", "dtchain"); // 铸币资格大唐
            } else if ("wc".equals(byId.getChainCode())) {
                stringObjectMap.put("roleCode", "wcChain"); // 铸币资格SDK
            } else if ("avata".equals(byId.getChainCode())) {
                stringObjectMap.put("roleCode", "avata"); // 铸币资格天舟
            }else if ("szavata".equals(byId.getChainCode())) {
                stringObjectMap.put("roleCode", "szavata"); // 铸币资格神舟
            }else if ("hw".equals(byId.getChainCode())) {
                stringObjectMap.put("roleCode", "hw"); // 华为
            }
            String url = shopConfig.getApiGatewayUrl() + "/nft/outapi/nftUser/openUserRoleForRomate";
            JSONObject jsonObject = dataWebService.hutoolPostWebService(url, stringObjectMap,shopDetailByShopId.getApiUser(),shopDetailByShopId.getApiPassword());

            if (jsonObject.getInteger("code")!=Constant.SUCCESS_CODE) {
                return ServerResponseEntity.showFailMsg(jsonObject.getString("msg"));
            }

            blockchainShopService.update(Wrappers.lambdaUpdate(BlockchainShop.class).set(BlockchainShop::getIsCoin, 1)
                    .set(BlockchainShop::getUpdateTime, new Date()).eq(BlockchainShop::getShopId, byId.getShopId())
                    .eq(BlockchainShop::getBlockchainCode, byId.getChainCode()));
            blockchainShopService.removeCacheByShopIds(Lists.newArrayList(byId.getShopId()));
        }
        shopChainApply.setVerifyUserId(SecurityUtils.getSysUser().getUserId());
        shopChainApply.setUpdateTime(new Date());
        shopChainApplyService.updateById(shopChainApply);
        return ServerResponseEntity.success();
    }
    @GetMapping("/test")
    public ServerResponseEntity transferToOthersListfrozen() throws Exception {
//        Map<String, Object> reselt = new HashMap<>();
//        ArrayList<Object> objects = Lists.newArrayList();
//        objects.add(29981);
//        objects.add(29980);
//        reselt.put("ids", objects);
//        String url1 = shopConfig.getApiGatewayUrl() + "/nft/outapi/api/nftquery/updateDawsStatus?";
//        dataWebService.hutoolPostWebServiceList(url1, objects);
        Map<String, String> stringObjectMap = new HashMap<>();
        stringObjectMap.put("platformName","12");
        stringObjectMap.put("platformCode","dtc");
        stringObjectMap.put("chainCode","wc");
        stringObjectMap.put("shopId","12");
        stringObjectMap.put("shopName","test");
        String url = shopConfig.getApiGatewayUrl() + "/nft/outapi/nftUser/addForRomate?";
        JSONObject jsonObject = dataWebService.hutoolPostWebService(url, stringObjectMap,null,null);
        return ServerResponseEntity.success();
    }
    @GetMapping("/test1")
    public ServerResponseEntity transferToOthersListfroz() throws Exception {
//        Map<String, Object> reselt = new HashMap<>();
//        ArrayList<Object> objects = Lists.newArrayList();
//        objects.add(29981);
//        objects.add(29980);
//        reselt.put("ids", objects);
//        String url1 = shopConfig.getApiGatewayUrl() + "/nft/outapi/api/nftquery/updateDawsStatus?";
//        dataWebService.hutoolPostWebServiceList(url1, objects);
        Map<String, String> stringObjectMap = new HashMap<>();
        stringObjectMap.put("userPwd","SmJjjYUdVCogFVvT");
        stringObjectMap.put("userCode","20230427212832002");
        stringObjectMap.put("totalTimes","12");
        String url = shopConfig.getApiGatewayUrl() + "/nft/outapi/nftUser/rechargeForRomate?";
        JSONObject jsonObject = dataWebService.hutoolPostWebService(url, stringObjectMap,null,null);
        return ServerResponseEntity.success();
    }

    @GetMapping("/test2")
    public ServerResponseEntity test2() throws Exception {

        List<Map<String,Object>> list = new ArrayList<>();

        Map<String,Object> map1 = new HashMap<>();
        map1.put("shopId",1000);
        map1.put("shopName","测试接口要删的1");
        map1.put("chainCode","dt");

        Map<String,Object> map2 = new HashMap<>();
        map2.put("shopId",1000);
        map2.put("shopName","测试接口要删的1");
        map2.put("chainCode","wc");

        list.add(map1);
        list.add(map2);

        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("platformCode","dtc");
        stringObjectMap.put("platformName","大头菜");
        stringObjectMap.put("shops",list);
        //String url = "http://localhost:8080/nft/nftUser/addUserForRomate?";
        //JSONObject jsonObject = dataWebService.hutoolPostWebService(url, stringObjectMap);

        Map<String, String> stringObjectMap1 = new HashMap<>();
        stringObjectMap1.put("platformCode","dtc");
        stringObjectMap1.put("shopId","1000");
        stringObjectMap1.put("userName","20230511170304001");
        stringObjectMap1.put("userPassword","EwGZCZngzdqgVpvO");
        stringObjectMap1.put("chainCode","dt");
        stringObjectMap1.put("roleCode","zhubi");

        //String url = "http://localhost:8080/nft/nftUser/openUserRoleForRomate";
        //JSONObject jsonObject = dataWebService.hutoolPostWebService(url, stringObjectMap1);

        Map<String, String> stringObjectMap2 = new HashMap<>();
        stringObjectMap2.put("userCode","20230511170304001");
        stringObjectMap2.put("userPwd","EwGZCZngzdqgVpvO");
        stringObjectMap2.put("totalTimes","100");
        stringObjectMap2.put("createTime","2023-05-11 15:08:37");

        //String url = "http://localhost:8080/nft/nftUser/rechargeForRomate";
        //JSONObject jsonObject = dataWebService.hutoolPostWebService(url, stringObjectMap2);

        Map<String, String> stringObjectMap3 = new HashMap<>();
        stringObjectMap3.put("dtcName","20230511170304001");
        stringObjectMap3.put("passWord","EwGZCZngzdqgVpvO");

        //String url = "http://localhost:8080/nft/nftUser/getInfoForRomate";
        //JSONObject jsonObject = dataWebService.hutoolPostWebService(url, stringObjectMap3);

        Map<String, String> stringObjectMap4 = new HashMap<>();
        stringObjectMap4.put("creditCode", "hyg");
        stringObjectMap4.put("channelCode", "hyg");
        String url = "http://localhost:8080/personnft/app/prodInfo/synchronizationAllProdData?";

        JSONObject jsonObject1 = dataWebService.hutoolGetWebService(url, stringObjectMap4,null,null);
        if(jsonObject1.getInteger("code")!= Constant.SUCCESS_CODE){
            throw new YamiShopBindException(jsonObject1.getString("msg"));
        }
        List<Product> prodInfoList =JSONObject.parseArray(jsonObject1.getJSONArray("data").toJSONString(), Product.class);

        return ServerResponseEntity.success();
    }
}
